import socket
import threading


def t_send_main(client_socket):
    # 向客户端发送信息
    while True:
        send_msg = input("请输入要发送信息")
        send_bytes = send_msg.encode("gbk")
        client_socket.send(send_bytes)


def t_recv_main(client_socket):
    # 接收客户端信息
    while True:
        recv_bytes = client_socket.recv(1024)
        recv_msg = recv_bytes.decode("gbk")
        print(f"接收到信息{recv_msg}")


def main():
    # 构造服务器套接字实例
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # 绑定ip 和 端口
    SERVER_ADDR = ("192.168.11.151", 60000)
    server_socket.bind(SERVER_ADDR)

    # 开始侦听
    server_socket.listen()

    # 接收客户端连接
    client_socket, client_addr = server_socket.accept()
    print(f"{client_addr}成功连接")

    # 向客户端发送信息
    t_send = threading.Thread(target=t_send_main, args=(client_socket, ))
    t_send.start()

    # 接收客户端信息
    t_recv = threading.Thread(target=t_recv_main, args=(client_socket, ))
    t_recv.start()


if __name__ == '__main__':
    main()
